﻿using SqlSugar;
using System.Linq;
using System.Linq.Expressions;
using Zhp.Common.Helper;
using Zhp.SqlSugar.UnitOfWork;

namespace Zhp.SqlSugar.Entity
{
    /// <summary>
    /// 扩展静态方法查询
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public abstract class RootEntityReadOnly<TEntity> : RootEntity where TEntity : class, new()
    {
        private static ISqlSugarClient context = ApplicationHelper.GetService<IUnitOfWork<SugarUnitOfWork>>().Db;

        #region 其他查询方法

        public static async Task<List<TEntity>> Where(Expression<Func<TEntity, bool>> exp)
        {
            return await context.Queryable<TEntity>().Where(exp).ToListAsync();
        }

        /// <summary>
        /// 没有返回Null
        /// select top 1 * from Student where id=1
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static async Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> exp)
        {
            return await context.Queryable<TEntity>().FirstAsync(exp);
        }

        /// <summary>
        /// 没有返回Null，如果结果大于1条会抛出错误
        /// select * from Student where id=1 查询id等于1的单条记录
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> exp)
        {
            return await context.Queryable<TEntity>().SingleAsync(exp);
        }

        /// <summary>
        /// 全表查询
        /// </summary>
        /// <returns></returns>
        public static List<TEntity> FindAll()
        {
            return context.Queryable<TEntity>().ToList();
        }

        /// <summary>
        /// 全表查询
        /// </summary>
        /// <returns></returns>
        public static async Task<List<TEntity>> FindAllAsync()
        {
            return await context.Queryable<TEntity>().ToListAsync();
        }

        /// <summary>
        /// 基础查询
        /// </summary>
        /// <returns></returns>
        public static ISugarQueryable<TEntity> Query()
        {
            return context.Queryable<TEntity>();
        }

        #endregion
    }

    /// <summary>
    /// 基础公共字段
    /// </summary>
    public abstract class RootEntity : IRootEntity
    {
        /// <summary>
        /// 是否删除
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "软删除(1:删除，0:未删除)")]
        public virtual bool IsDeleted { get; set; } = false;

        /// <summary>
        /// 可携带其他数据新增
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<IRootEntity> InsertExtraEntity { get; set; } = new List<IRootEntity>();

        /// <summary>
        /// 可携带其他数据更新
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<IRootEntity> UpdateExtraEntity { get; set; } = new List<IRootEntity>();

        /// <summary>
        /// 可携带其他数据一起删除
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<IRootEntity> DeleteExtraEntity { get; set; } = new List<IRootEntity>();
    }

    public interface IRootEntity : IDeleted
    {
        /// <summary>
        /// 可携带其他数据新增
        /// </summary>
        public List<IRootEntity> InsertExtraEntity { get; set; }

        /// <summary>
        /// 可携带其他数据更新
        /// </summary>
        public List<IRootEntity> UpdateExtraEntity { get; set; }

        /// <summary>
        /// 可携带其他数据一起删除
        /// </summary>
        public List<IRootEntity> DeleteExtraEntity { get; set; }
    }

    /// <summary>
    /// 继承此接口实体自动拥有逻辑删除功能
    /// </summary>
    public interface IDeleted
    {
        public bool IsDeleted { get; set; }
    }
}
